package com.arvin.controller;

import com.arvin.pojo.*;
import com.arvin.service.StudentService;
import com.arvin.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.SimpleTimeZone;

@RestController
@RequestMapping("/student")
@Tag(name = "学生端接口",description = "学生端接口")
@Slf4j
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 报名信息保存
     * @param student
     * @return
     */
    @Operation(summary = "报名信息保存",description = "报名信息保存")
    @PostMapping("/signup/saveInfo")
    public Result signup(@RequestBody Student student){
        Map<String ,Object> map = ThreadLocalUtil.get();
        String oldStudentId = (String) map.get("StudentId");// 获取原来的学号
        student.setOldStudentId(oldStudentId);

        if (!oldStudentId.equals(student.getStudentId())){
            Student student1 = studentService.getStudentById(student.getStudentId());
            if (student1 != null){
                return Result.error("保存失败，该学号已存在");
            }
            map.put("StudentId",student.getStudentId());
            ThreadLocalUtil.set(map); // 将新的学号重新存入线程
        }
        log.info("学生信息开始保存:{}",student);
        studentService.saveStudent(student);
        return Result.success();
    }

    /**
     * 获取学生报名信息：根据学号获取学生信息
     * @return
     */
    @Operation(summary = "获取学生报名信息",description = "根据学号获取学生信息")
    @GetMapping("/signup/getInfo/{studentId}")
    //通过学号查询学生信息，学号通过ThreadLocalUtil从登陆时产生的token中获取
    public Result<Student> getInfo(@PathVariable String studentId){
        Student student = studentService.findByStudentId(studentId);
        log.info("获取学生报名信息{}",student);
        return Result.success(student);
    }

    /**
     * 保存预约考试信息：添加和修改考试预约信息
     * @param exam
     * @return
     */
    @Operation(summary = "保存预约考试信息",description = "添加和修改考试预约信息")
    @PostMapping("/bookExam/saveExamInfo")
    public Result saveExamInfo(@RequestBody Exam exam){
        log.info("保存预约考试信息{}",exam);
        Exam exam1 = studentService.saveExamInfo(exam);
        return Result.success(exam1);
    }

    /**
     * 申请免试：保存免试信息
     * @param exempt
     * @return
     */
    @Operation(summary = "申请免试",description = "保存免试信息")
    @PostMapping("/bookExam/exempt")
    public Result exempt(@RequestBody Exempt exempt){
        log.info("免试信息保存{}",exempt);
        Exempt exempt1 = studentService.saveExempt(exempt);
        return Result.success(exempt1);
    }

    /**
     * 获取学生申请免试信息：获取学生填写的面试信息
     * @param exempId
     * @return
     */
    @GetMapping("/bookExam/getExempt/{exempId}")
    @Operation(summary = "获取学生申请免试信息",description = "获取学生填写的面试信息")
    public Result<Exempt> getExemptInfo(@PathVariable("exempId") Integer exempId){
        Exempt exempt = studentService.getExemptInfo(exempId);
        log.info("获取学生申请免试信息{}",exempt);
        return Result.success(exempt);
    }

    /**
     * 获取学生预约考试时间信息：获取学生的预约考试时间用于页面回显
     * @param examId
     * @return
     */
    @GetMapping("/bookExam/getExamInfo/{examId}")
    @Operation(summary = "获取学生预约考试时间信息",description = "获取学生的预约考试时间用于页面回显")
    public Result<Exam> getBookExamInfo(@PathVariable("examId") Integer examId){
        Exam exam = studentService.findByExamId(examId);
        log.info("获取学生预约考试时间信息{}",exam);
        return Result.success(exam);
    }

    /**
     * 获取时间: 获取管理员设置的预约考试时间，预约面试时间和免试通过后的面试时间
     * @param timeId
     * @return
     */
    @GetMapping("/bookExam/getTime/{timeId}")
    @Operation(summary = "获取时间",description = "获取管理员设置的预约考试时间，预约面试时间和免试通过后的面试时间")
    public Result<Time> getBookExamTime(@PathVariable("timeId") Integer timeId){
        Time time = studentService.findByTimeId(timeId);
        log.info("获取管理员设置的预约考试时间，预约面试时间和免试通过后的面试时间{}",time);
        return Result.success(time);
    }

    /**
     * 保存预约面试信息:添加和修改面试预约信息
     * @param interview
     * @return
     */
    @PostMapping("/bookInterview/saveInfo")
    @Operation(summary = "保存预约面试信息",description = "添加和修改面试预约信息")
    public Result saveInterviewInfo(@RequestBody Interview interview){
        log.info("保存预约面试信息{}",interview);
        Interview interview1 = studentService.saveInterviewInfo(interview);
        return Result.success(interview1);
    }

    /**
     * 获取学生预约面试时间信息:获取学生的预约面试时间用于页面回显
     * @param intvId
     * @return
     */
    @GetMapping("/bookInterview/getInfo/{intvId}")
    @Operation(summary = "获取学生预约面试时间信息",description = "获取学生的预约面试时间用于页面回显")
    public Result<Interview> getBookIntvInfo(@PathVariable("intvId") Integer intvId){
        Interview interview = studentService.findByIntvId(intvId);
        log.info("获取学生预约面试时间信息{}",interview);
        return Result.success(interview);
    }

    /**
     * 通过考试日期获取预约面试时间信息
     * @param intvDate
     * @return
     */
    @GetMapping("/bookInterview/getIntvInfoByDate/{intvDate}")
    @Operation(summary = "通过考试日期获取预约面试时间信息",description = "通过考试日期获取预约面试时间信息")
    public Result<List<Interview>> getIntvInfoByDate(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate intvDate){
        Map<String,String> map = ThreadLocalUtil.get();
        String studentId = map.get("StudentId");
        List<Interview> interviewList = studentService.findByIntvDate(intvDate,studentId);
        log.info("获取学生预约面试时间信息{}",interviewList);
        return Result.success(interviewList);
    }

    /**
     * 通过免试日期获取预约免试时间信息
     * @param exemDate
     * @return
     */
    @GetMapping("/bookExam/getExemInfoByDate/{exemDate}")
    @Operation(summary = "通过免试日期获取预约免试时间信息",description = "通过免试日期获取预约免试时间信息")
    public Result<List<Exempt>> getExemInfoByDate(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate exemDate){
        Map<String,String> map = ThreadLocalUtil.get();
        String studentId = map.get("StudentId");
        List<Exempt> interviewList = studentService.findByExemDate(exemDate,studentId);
        log.info("获取学生预约面试时间信息{}",interviewList);
        return Result.success(interviewList);
    }

    /**
     * 通过考试id清除考试信息
     * @param examId
     * @return
     */
    @Operation(summary = "通过考试id清除考试信息",description = "通过考试id清除考试信息")
    @DeleteMapping("/bookExam/clearByExamId/{examId}")
    public Result clearByExamId(@PathVariable Integer examId){
        studentService.clearByExamId(examId);
        return Result.success();
    }

    /**
     * 通过免试id清除免试信息
     * @param exemId
     * @return
     */
    @Operation(summary = "通过免试id清除免试信息",description = "通过免试id清除免试信息")
    @DeleteMapping("/bookExam/clearByExemId/{exemId}")
    public Result clearByExemId(@PathVariable Integer exemId){
        studentService.clearByExemId(exemId);
        return Result.success();
    }

    /**
     * 通过面试id清除面试信息
     * @param intvId
     * @return
     */
    @Operation(summary = "通过面试id清除面试信息",description = "通过面试id清除面试信息")
    @DeleteMapping("/bookInterview/clearByIntvId/{intvId}")
    public Result clearByIntvId(@PathVariable Integer intvId){
        studentService.clearByIntvId(intvId);
        return Result.success();
    }
}
